package twoPointers;

//https://leetcode.cn/problems/happy-number/
public class l_202 {
    //计算n的每一位平方和
    public int bitSum(int n) {
        int ans = 0;
        while(n > 0) {
            ans += (int) Math.pow(n % 10, 2);
            n/=10;
        }
        return ans;
    }

    //快慢指针 false说明有环, true也说明有环不过是全为1的环
    public boolean isHappy(int n) {
        int fast = n, slow = n;
        do {
            fast = bitSum(fast);
            fast = bitSum(fast);
            slow = bitSum(slow);
        } while(fast != slow) ;
        if(fast == 1) return true;
        return false;
    }
}
